using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Templates : BasePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer }; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowTemplates();
        }
    }

    private void HideAll()
    {
        //placehoders
        phTemplates.Visible = false;
        phTemplateDetails.Visible = false;
        phAddNewTemplate.Visible = false;
        phTemplateFiles.Visible = false;
        phTemplateFileDetails.Visible = false;
        phTemplateControls.Visible = false;
        phTemplateControlDetails.Visible = false;

        //links
        btnAddNewTemplate.Visible = false;
        btnSaveNewTemplate.Visible = false;
        btnCancelNewTemplate.Visible = false;

        btnAllTemplates.Visible = false;

        btnEditTemplate.Visible = false;
        btnDeleteTemplate.Visible = false;
        btnSaveTemplate.Visible = false;
        btnCancelTemplate.Visible = false;
        
        btnAddTemplateFile.Visible = false;
        btnSaveTemplateFile.Visible = false;
        btnCancelTemplateFile.Visible = false;

        btnAddTemplateControl.Visible = false;
        btnSaveTemplateControl.Visible = false;
        btnCancelTemplateControl.Visible = false;
    }

    private void ShowTemplates()
    {
        HideAll();
        if (dvTemplate.CurrentMode != DetailsViewMode.ReadOnly)
        {
            dvTemplate.ChangeMode(DetailsViewMode.ReadOnly);
        }
        phTemplates.Visible = true;
        btnAddNewTemplate.Visible = true;
    }

    private void ShowTemplateDetails()
    {
        HideAll();
        phTemplateDetails.Visible = true;
        phTemplateFiles.Visible = true;
        phTemplateControls.Visible = true;

        btnEditTemplate.Visible = true;
        btnDeleteTemplate.Visible = true;
        btnAllTemplates.Visible = true;
        btnAddTemplateFile.Visible = true;
        btnAddTemplateControl.Visible = true;
    }

    private void EnsureTemplateDetailsReadOnly()
    {
        dvTemplate.ChangeMode(DetailsViewMode.ReadOnly);
        btnSaveTemplate.Visible = false;
        btnCancelTemplate.Visible = false;
    }

    protected void gvTemplates_SelectedIndexChanged(object sender, EventArgs e)
    {
        dvTemplate.PageIndex = gvTemplates.SelectedIndex;
        ShowTemplateDetails();
        dvTemplate.DataBind();
        gvTemplateFiles.DataBind();
        gvTemplateControls.DataBind();
    }

    protected void dvTemplate_PageIndexChanged(object sender, EventArgs e)
    {
        gvTemplateControls.SelectedIndex = dvTemplate.PageIndex;
        dvTemplate.DataBind();
        gvTemplateFiles.DataBind();
        gvTemplateControls.DataBind();
    }

    protected void btnAllTemplates_Click(object sender, EventArgs e)
    {
        ShowTemplates();
    }

    protected void btnEditTemplate_Click(object sender, EventArgs e)
    {
        dvTemplate.ChangeMode(DetailsViewMode.Edit);
        dvTemplate.AllowPaging = false;
        btnAllTemplates.Visible = false;
        btnEditTemplate.Visible = false;
        btnDeleteTemplate.Visible = false;
        btnAddTemplateFile.Visible = false;
        btnAddTemplateControl.Visible = false;

        btnSaveTemplate.Visible = true;
        btnCancelTemplate.Visible = true;
    }

    protected void btnDeleteTemplate_Click(object sender, EventArgs e)
    {
        dvTemplate.DeleteItem();
        ShowTemplates();
    }

    protected void btnCancelTemplate_Click(object sender, EventArgs e)
    {
        dvTemplate.ChangeMode(DetailsViewMode.ReadOnly);
        dvTemplate.AllowPaging = true;

        btnAllTemplates.Visible = true;
        btnEditTemplate.Visible = true;
        btnDeleteTemplate.Visible = true;
        btnAddTemplateFile.Visible = true;
        btnAddTemplateControl.Visible = true;

        btnSaveTemplate.Visible = false;
        btnCancelTemplate.Visible = false;
    }

    protected void btnSaveTemplate_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            dvTemplate.UpdateItem(true);
            dvTemplate.AllowPaging = true;

            btnAllTemplates.Visible = true;
            btnEditTemplate.Visible = true;
            btnDeleteTemplate.Visible = true;
            btnAddTemplateFile.Visible = true;
            btnAddTemplateControl.Visible = true;

            btnSaveTemplate.Visible = false;
            btnCancelTemplate.Visible = false;
        }
    }

    protected void gvTemplateFiles_SelectedIndexChanged(object sender, EventArgs e)
    {
        dvTemplate.AllowPaging = false;

        btnAllTemplates.Visible = false;
        btnEditTemplate.Visible = false;
        btnDeleteTemplate.Visible = false;
        btnAddTemplateFile.Visible = false;
        btnAddTemplateControl.Visible = false;

        if(dvTemplate.CurrentMode != DetailsViewMode.ReadOnly)
        {
            dvTemplate.ChangeMode(DetailsViewMode.ReadOnly);
            btnSaveTemplate.Visible = false;
            btnCancelTemplate.Visible = false;
        }

        dvTemplateFileDetails.PageIndex = gvTemplateFiles.SelectedIndex;
        dvTemplateFileDetails.ChangeMode(DetailsViewMode.Edit);
        dvTemplateFileDetails.DataBind();

        btnSaveTemplateFile.Visible = true;
        btnCancelTemplateFile.Visible = true;
        btnSaveTemplateControl.Visible = false;
        btnCancelTemplateControl.Visible = false;

        phTemplateFiles.Visible = false;
        phTemplateFileDetails.Visible = true;
        phTemplateControls.Visible = true;
        phTemplateControlDetails.Visible = false;

    }

    protected void btnAddTemplateFile_Click(object sender, EventArgs e)
    {
        dvTemplate.AllowPaging = false;

        btnAllTemplates.Visible = false;
        btnEditTemplate.Visible = false;
        btnDeleteTemplate.Visible = false;
        btnAddTemplateFile.Visible = false;
        btnAddTemplateControl.Visible = false;

        btnSaveTemplateFile.Visible = true;
        btnCancelTemplateFile.Visible = true;

        phTemplateFiles.Visible = false;
        phTemplateFileDetails.Visible = true;

        dvTemplateFileDetails.ChangeMode(DetailsViewMode.Insert);
    }

    protected void btnCancelTemplateFile_Click(object sender, EventArgs e)
    {
        dvTemplate.AllowPaging = true;

        phTemplateFiles.Visible = true;
        phTemplateFileDetails.Visible = false;

        btnAllTemplates.Visible = true;
        btnEditTemplate.Visible = true;
        btnDeleteTemplate.Visible = true;
        btnAddTemplateFile.Visible = true;
        btnAddTemplateControl.Visible = true;

        btnSaveTemplateFile.Visible = false;
        btnCancelTemplateFile.Visible = false;
    }

    protected void btnSaveTemplateFile_Click(object sender, EventArgs e)
    {
        if (dvTemplateFileDetails.CurrentMode == DetailsViewMode.Insert)
        {
            dvTemplateFileDetails.InsertItem(true);
        }
        else if (dvTemplateFileDetails.CurrentMode == DetailsViewMode.Edit)
        {
            dvTemplateFileDetails.UpdateItem(true);
        }

        dvTemplate.AllowPaging = true;

        phTemplateFiles.Visible = true;
        phTemplateFileDetails.Visible = false;

        btnAllTemplates.Visible = true;
        btnEditTemplate.Visible = true;
        btnDeleteTemplate.Visible = true;
        btnAddTemplateFile.Visible = true;
        btnAddTemplateControl.Visible = true;

        btnSaveTemplateFile.Visible = false;
        btnCancelTemplateFile.Visible = false;
    }

    protected void btnAddNewTemplate_Click(object sender, EventArgs e)
    {
        HideAll();
        phAddNewTemplate.Visible = true;
        btnSaveNewTemplate.Visible = true;
        btnCancelNewTemplate.Visible = true;
    }

    protected void btnCancelNewTemplate_Click(object sender, EventArgs e)
    {
        ShowTemplates();
    }

    protected void btnSaveNewTemplate_Click(object sender, EventArgs e)
    {
        dvNewTemplate.InsertItem(true);
        ShowTemplates();
    }

    protected void gvTemplateControls_SelectedIndexChanged(object sender, EventArgs e)
    {
        dvTemplate.AllowPaging = false;

        btnAllTemplates.Visible = false;
        btnEditTemplate.Visible = false;
        btnDeleteTemplate.Visible = false;
        btnAddTemplateFile.Visible = false;
        btnAddTemplateControl.Visible = false;

        if (dvTemplate.CurrentMode != DetailsViewMode.ReadOnly)
        {
            dvTemplate.ChangeMode(DetailsViewMode.ReadOnly);
            btnSaveTemplate.Visible = false;
            btnCancelTemplate.Visible = false;
        }

        dvTemplateControl.PageIndex = gvTemplateControls.SelectedIndex;
        dvTemplateControl.ChangeMode(DetailsViewMode.Edit);
        dvTemplateControl.DataBind();

        btnSaveTemplateFile.Visible = false;
        btnCancelTemplateFile.Visible = false;
        btnSaveTemplateControl.Visible = true;
        btnCancelTemplateControl.Visible = true;

        phTemplateFiles.Visible = true;
        phTemplateFileDetails.Visible = false;
        phTemplateControls.Visible = false;
        phTemplateControlDetails.Visible = true;

    }

    protected void btnAddTemplateControl_Click(object sender, EventArgs e)
    {
        dvTemplate.AllowPaging = false;

        phTemplateControls.Visible = false;
        phTemplateControlDetails.Visible = true;

        btnAllTemplates.Visible = false;
        btnEditTemplate.Visible = false;
        btnDeleteTemplate.Visible = false;
        btnAddTemplateFile.Visible = false;
        btnAddTemplateControl.Visible = false;
        btnSaveTemplateControl.Visible = true;
        btnCancelTemplateControl.Visible = true;

        dvTemplateControl.ChangeMode(DetailsViewMode.Insert);
    }

    protected void btnCancelTemplateControl_Click(object sender, EventArgs e)
    {
        dvTemplate.AllowPaging = true;
        
        phTemplateControls.Visible = true;
        phTemplateControlDetails.Visible = false;

        btnAllTemplates.Visible = true;
        btnEditTemplate.Visible = true;
        btnDeleteTemplate.Visible = false;
        btnAddTemplateFile.Visible = true;
        btnAddTemplateControl.Visible = true;
        btnSaveTemplateControl.Visible = false;
        btnCancelTemplateControl.Visible = false;
    }

    protected void btnSaveTemplateControl_Click(object sender, EventArgs e)
    {
        if (dvTemplateControl.CurrentMode == DetailsViewMode.Insert)
        {
            dvTemplateControl.InsertItem(true);
        }
        else if (dvTemplateControl.CurrentMode == DetailsViewMode.Edit)
        {
            dvTemplateControl.UpdateItem(true);
        }

        dvTemplate.AllowPaging = true;

        phTemplateControls.Visible = true;
        phTemplateControlDetails.Visible = false;

        btnAllTemplates.Visible = true;
        btnEditTemplate.Visible = true;
        btnDeleteTemplate.Visible = false;
        btnAddTemplateFile.Visible = true;
        btnAddTemplateControl.Visible = true;

        btnSaveTemplateControl.Visible = false;
        btnCancelTemplateControl.Visible = false;
    }

    protected void dsTemplates_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Template> e)
    {
        e.SetData(Acms.Bll.Template.GetAllBySiteId(AcmsSite.Id));
    }

    protected void dsTemplates_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.Template> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.BeginTransaction();
            e.Item.SiteId = AcmsSite.Id;
        }
    }

    protected void dsTemplates_UpdatedData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.Template> e)
    {
        if(e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            TextBox txt = dvNewTemplate.FindControl("txtFilePath") as TextBox;
            if (txt != null)
            {
                Acms.Bll.TemplateFile tmp = Acms.Bll.TemplateFile.CreateInstance(e.Item, "Default");
                tmp.IsDefault = true;
                tmp.Path = txt.Text;
                tmp.UseTransaction(e.Item.TransactionKey);
                tmp.Update();
                e.Item.CommitTransaction();
            }
            else
            {
                e.Item.RollbackTransaction();
            }
        }
    }

    protected void dsTemplateFiles_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.TemplateFile> e)
    {
        int id = (int)dvTemplate.DataKey["Id"];
        e.SetData(Acms.Bll.TemplateFile.GetAllByTemplateId(id));
    }

    protected void dsTemplateFiles_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.TemplateFile> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.TemplateId = (int)dvTemplate.DataKey["Id"];
        }
    }

    protected void dsTemplateProperties_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.TemplateControl> e)
    {
        e.SetData(Acms.Bll.TemplateControl.GetAllByTemplateId((int)dvTemplate.DataKey["Id"]));
    }

    protected void dsTemplateProperties_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.TemplateControl> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.TemplateId = (int)dvTemplate.DataKey["Id"];
        }
    }

    protected void dsPropertyType_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.ControlType> e)
    {
        e.SetData(Acms.Bll.ControlType.GetAllBySiteId(AcmsSite.Id));
    }
}
